package com.kit.data.kitopeso.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class KitoSpaceItemDecoration extends RecyclerView.ItemDecoration {

    private final int horizontalSpacing;
    private final int verticalSpacing;

    private Drawable mDivider;

    private int dividerHeight;

    public KitoSpaceItemDecoration(int horizontalSpacingDp, int verticalSpacingDp, Context context) {
        this.horizontalSpacing = dpToPx(horizontalSpacingDp, context);
        this.verticalSpacing = dpToPx(verticalSpacingDp, context);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);


        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager || layoutManager instanceof StaggeredGridLayoutManager) {

            handleGridSpacing(outRect, parent.getChildAdapterPosition(view), layoutManager);
        } else if (layoutManager instanceof LinearLayoutManager) {

            handleLinearSpacing(outRect, parent.getChildAdapterPosition(view), layoutManager);
        }
    }

    private void handleLinearSpacing(Rect outRect, int position, RecyclerView.LayoutManager layoutManager) {
        int orientation = ((LinearLayoutManager) layoutManager).getOrientation();
        if (orientation == LinearLayoutManager.VERTICAL) {
            outRect.left = horizontalSpacing;
            outRect.right = horizontalSpacing;
            outRect.bottom = verticalSpacing;
            if (position == 0) outRect.top = verticalSpacing;
        } else {
            outRect.top = verticalSpacing;
            outRect.bottom = verticalSpacing;
            outRect.right = horizontalSpacing;
            if (position == 0) outRect.left = horizontalSpacing;
        }
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        if (verticalSpacing == 0 && mDivider != null) {
            drawHorizontal(c, parent);
        }

    }


    public void setmDivider(Drawable mDivider) {
        this.mDivider = mDivider;
    }

    public void setDividerHeight(int dividerHeight) {
        this.dividerHeight = dividerHeight;
    }

    private void drawHorizontal(Canvas c, RecyclerView parent) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + dividerHeight;
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }


    private void handleGridSpacing(Rect outRect, int position, RecyclerView.LayoutManager layoutManager) {
        int spanCount = getSpanCount(layoutManager);
        int column = position % spanCount;

        outRect.left = horizontalSpacing - column * horizontalSpacing / spanCount;
        outRect.right = (column + 1) * horizontalSpacing / spanCount;
        outRect.bottom = verticalSpacing;
        if (position < spanCount) outRect.top = verticalSpacing;

        if (horizontalSpacing == 0) {
            if (position == 0) {
                outRect.right = verticalSpacing;
            } else {
                outRect.left = verticalSpacing;
            }
        }

    }


    private int dpToPx(int dp, Context context) {
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()
        );
    }


    private int getSpanCount(RecyclerView.LayoutManager layoutManager) {
        if (layoutManager instanceof GridLayoutManager) {
            return ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            return ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
        }
        return 1;
    }

}
